#include "ResourcesManager.h"


ResourcesManager::ResourcesManager(void)
{
}


ResourcesManager::~ResourcesManager(void)
{
	Clear();
}

ResourcesManager* ResourcesManager::Instance()
{
	static ResourcesManager* m_instance = NULL;
	if(!m_instance)
		m_instance = new ResourcesManager();
	return m_instance;
}

void ResourcesManager::Init( ID3D10Device* device )
{
	Instance()->SetDevice(device);
}

void ResourcesManager::SetDevice( ID3D10Device* device )
{
	m_Device = device;
}

ID3D10ShaderResourceView* ResourcesManager::CreateTexture( const std::string& texName )
{
	HRESULT hr;
	ID3D10ShaderResourceView* ret;
	std::wstring name = ToWstring(texName);
	hr = D3DX10CreateShaderResourceViewFromFile(m_Device,name.c_str()
		,0,0,&ret,0);
	if(FAILED(hr))
	{
		//::MessageBoxA(NULL,"Get Texture Error",0,0);
		name = ToWstring("./Resource/" + texName);
		hr = D3DX10CreateShaderResourceViewFromFile(m_Device,name.c_str()
			,0,0,&ret,0);
		if(FAILED(hr))
		{
			::MessageBoxA(NULL,"Get Texture Error",0,0);
			exit(0);
		}
	}


	return ret;
}

ID3D10ShaderResourceView* ResourcesManager::GetTexture( const std::string& texName )
{

	std::map<std::string,ID3D10ShaderResourceView*>::const_iterator iter;
	iter = m_TextureMap.find(texName);
	if(iter != m_TextureMap.end())
		return iter->second;

	ID3D10ShaderResourceView* ret;
	ret = CreateTexture(texName);

	m_TextureMap.insert(make_pair(texName,ret));

	return ret;
}

void ResourcesManager::Clear()
{
	std::map<std::string,ID3D10ShaderResourceView*>::iterator iter;
	for(iter = m_TextureMap.begin() ;iter != m_TextureMap.end() ;++iter)
		ReleaseCOM(iter->second);
}
